import axios from 'axios';
import store from '@/store';
import { getToken, getClient } from '@/utils/auth';

const service = axios.create({
    baseURL: 'http://127.0.0.1:7001',
    headers: {
        'Content-Type': 'application/json',
    },
});

// 请求拦截器
service.interceptors.request.use(
    (config) => {
        config.headers.client = getClient();
        if (store.getters.token) {
            config.headers.authorization = `Bearer ${getToken()}`;
        }
        return config;
    },
    error => Promise.reject(error),
);
// 响应拦截器
service.interceptors.response.use(
    (response) => {
        const { data } = response;
        return data;
    }, error => Promise.reject(error),
);

export default service;
